<template>
  <el-row :gutter="20" style="padding: 20px">
    <el-col :span="6">
      <category @tree-node-click="treeNodeClick"></category>
    </el-col>
    <el-col :span="18">
      <div class="mod-config">
        <el-form :inline="true" size="mini" :model="form" @keyup.enter.native="loadData()">
          <el-form-item>
            <el-input v-model="form.key" placeholder="规格名称" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="loadData()">查询</el-button>
            <el-button type="primary" size="mini" icon="el-icon-c-scale-to-original" @click="getAllDataList()">展示全部</el-button>
            <el-button
              type="primary" size="mini" plain icon="el-icon-circle-plus-outline"
              @click="handleAdd()"
            >新增</el-button>
            <el-button
              type="danger" size="mini" plain icon="el-icon-delete"
              @click="deleteHandle()"
              :disabled="selections.length <= 0"
            >批量删除</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataList"
          border
          v-loading="loading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;"
        >
          <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="name" header-align="center" align="center" label="属性名"></el-table-column>
          <el-table-column v-if="attrType === 1" prop="groupName" header-align="center" align="center" label="所属分组"></el-table-column>
          <el-table-column prop="valueType" header-align="center" align="center" label="值类型">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.valueType === 0">多选</el-tag>
              <el-tag v-else>单选</el-tag>
            </template>
          </el-table-column>
          <el-table-column header-align="center" align="center" label="图标">
            <template slot-scope="scope">
              <image-preview :src="scope.row.icon"/>
            </template>
          </el-table-column>
          <el-table-column prop="optionalValue" header-align="center" align="center" label="可选值">
            <template slot-scope="scope">
              <el-tooltip placement="top">
                <div slot="content">
                  <span v-for="(i,index) in scope.row.optionalValue" :key="index">{{i}}<br/></span>
                </div>
                <el-tag size="small">{{scope.row.optionalValue[0]}}</el-tag>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column prop="categoryName" header-align="center" align="center" label="所属分类"></el-table-column>
          <el-table-column v-if="attrType === 1" prop="searchType" header-align="center" align="center" label="可检索"
          >
            <template slot-scope="scope">
              <i class="el-icon-success" v-if="scope.row.searchType === 1"></i>
              <i class="el-icon-error" v-else></i>
            </template>
          </el-table-column>
          <el-table-column prop="enable" header-align="center" align="center" label="启用">
            <template slot-scope="scope">
              <i class="el-icon-success" v-if="scope.row.enable === 1"></i>
              <i class="el-icon-error" v-else></i>
            </template>
          </el-table-column>
          <el-table-column v-if="attrType === 1" prop="quickShow" header-align="center" align="center" label="快速展示">
            <template slot-scope="scope">
              <i class="el-icon-success" v-if="scope.row.quickShow === 1"></i>
              <i class="el-icon-error" v-else></i>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="handleUpdate(scope.row.id)">修改</el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="loadData"
        />
        <attr-form :type="attrType" ref="dataForm" @ok="loadData"></attr-form>
      </div>
    </el-col>
  </el-row>
</template>

<script>

import Category from "../../common/category.vue";
import AttrForm from "./attrForm.vue";
import { deleteAttr, findAttr } from '@/api/product/attr'
import {getCategoryPathById} from "@/api/product/category";
export default {
  components: { Category, AttrForm },
  props: {
    attrType: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      form: {},
      dataList: [],
      categoryPath:[],
      queryParams: {
        pageNo: 1,
        pageSize: 10
      },
      total: 0,
      loading: false,
      selections: []
    };
  },
  created() {
    this.queryParams.attrType = this.attrType
    this.loadData();
  },
  methods: {
    treeNodeClick(data, node, component) {
      if (node.level === 3) {
        this.queryParams.categoryId = data.id;
        getCategoryPathById(data.id).then(res => {
          if (res.success) {
            this.categoryPath = res.data.map(item => item.toString())
          }
        })
        this.loadData();
      }
    },
    getAllDataList(){
      delete this.queryParams.categoryId
      this.loadData();
    },
    loadData() {
      this.loading = true;
      findAttr(this.queryParams).then(res => {
        if (res.success) {
          this.dataList = res.data.records;
          this.total = res.data.total;
        }
        this.loading = false;
      });
    },
    selectionChangeHandle(val) {
      this.selections = val;
    },
    handleUpdate(id) {
      this.$nextTick(() => {
        this.$refs.dataForm.edit(id);
      });
    },
    handleAdd() {
      this.$nextTick(() => {
        this.$refs.dataForm.add(this.categoryPath);
      });
    },
    // 删除
    deleteHandle(id) {
      let ids = id ? [id] : this.selections.map(item => {return item.attrId;});
      this.$confirm(
        `确定对选中的值进行删除操作吗?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        deleteAttr(ids).then(res => {
          if (res.success) {
            this.$message.success("删除成功")
            this.loadData()
          }
        });
      });
    }
  }
};
</script>
